<canvas id="bar-chart" width="150" height="150"></canvas>
<script src="jsuri.js"></script> 
<script type='text/javascript'>
var ctx = document.getElementById('bar-chart').getContext('2d');

//chrome.extension.getBackgroundPage().cleanStorage();
var habitArrayIndex = 0;
var habitArray = new Array(); 

function Object(time, tabid, url, status) {
    this.time = time;
    this.tabid = tabid;
    this.url = url;
    this.status = status;
}

function setObject(time, tabid, url, status) {
    habitArray[habitArrayIndex++] = new Object(time, tabid, url, status);
}

var dayArrayIndex = 0;
var daytimeArray = new Array();
var length = chrome.extension.getBackgroundPage().getSize();
document.write('<br>' + length + '<br>');

for (i=0; i<length; ++i) {
    var key = chrome.extension.getBackgroundPage().getKey(i);
    var value = chrome.extension.getBackgroundPage().getItem(key);
    array = key.split(",");
    setObject(array[0], array[1], array[2], value);
}

function sortByURL(a, b) {
    return a.url - b.url;
}

function sortByTime(a, b) {
    return a.time - b.time;
}

function convertSecondsToDaytime(time)
{
    var t = new Date(parseInt(time));
    return t.getHours() + ':' + t.getSeconds();
}

function getDomain(url) {
    var uri = new jsUri(url);
    return uri.host();
}

//habitArray.sort(sortByTime);

//for (i=0; i<length; ++i) {
//    document.write(habitArray[i].time + ' ' + habitArray[i].url + ' '+ habitArray[i].status + '<br>');
//}

for (i=0; i<length; ++i) {
    var daytime = convertSecondsToDaytime(habitArray[i].time);
    var url = getDomain(habitArray[i].url);
    document.write('daytime ' + daytime + ' url ' + url + '<br>');
    if (typeof(daytimeArray[daytime]) == 'undefined') {
        var sites = new Array();
        sites[url] = 1;
        daytimeArray[''+daytime] = urlArray;
        document.write('daytimeArray[' + daytime + '] ' + daytimeArray[daytime] + '<br>');
        document.write('add time ' + daytime + '<br>');
    } else {
        var sites = daytimeArray[daytime];
        if (typeof(sites[url]) == 'undefined') {
            sites[url] = 1;
            daytimeArray[daytime] = sites;
            document.write('add url ' + url + ' to time ' + daytime + '<br>');
        }
    }
}

for (var item in daytimeArray) {
    document.write(item + ' - ' + daytimeArray[item].length + '<br>');
}

</script>
